package com.glink.manage.controller;

import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.ai.AiTaskDTO;
import com.glink.manage.service.AiService;
import com.glink.manage.vo.ai.AiVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;

@Slf4j
@RestController
@RequestMapping("/API/Manage/ai")
@Api(tags = "AI识别")
public class AiController extends AbstractBaseController {

    @Autowired
    private AiService aiService;

    @ApiOperation(value = "AI识别接口", notes = "通过Base64图片调用算法接口进行AI识别")
    @PostMapping(value = "/recognize", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<AiVO> recognize(@RequestBody AiTaskDTO aiTaskDTO) {
        log.info("AI识别任务请求: {}", aiTaskDTO);

        AiVO result = aiService.invokeAlgorithm(aiTaskDTO);

        log.info("AI识别任务请求: success");

        return this.response(result);  // 返回 VO 类型的 ResponseResult
    }

    public static void main(String[] args) {
        String base64Image = "";
        try {
            BufferedImage image = ImageIO.read(new File("E:\\qss\\test\\20250716100432296_08f9a44d841a43bcb28f148b5877b47f.jpg"));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", baos);
            byte[] imageBytes = baos.toByteArray();
            base64Image = Base64.getEncoder().encodeToString(imageBytes);
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(base64Image);
    }
}
